package dao;

import model.Activity;
import util.DBUtil;
import util.ExceptionUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 活动数据访问对象
 */
public class ActivityDAO {
    // 添加活动
    public void addActivity(Activity activity) {
        String sql = "INSERT INTO activities (activity_id, title, location, start_time, " +
                "end_time, sign_up_end_time, max_participants, current_participants) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, activity.getActivityId());
            pstmt.setString(2, activity.getTitle());
            pstmt.setString(3, activity.getLocation());
            pstmt.setTimestamp(4, activity.getStartTime());
            pstmt.setTimestamp(5, activity.getEndTime());
            pstmt.setTimestamp(6, activity.getSignUpEndTime());
            pstmt.setInt(7, activity.getMaxParticipants());
            pstmt.setInt(8, activity.getCurrentParticipants());

            pstmt.executeUpdate();
        } catch (SQLException e) {
            ExceptionUtil.handleSQLException(e);
        }
    }

    // 根据ID查询活动
    public Activity findActivityById(String activityId) {
        String sql = "SELECT * FROM activities WHERE activity_id = ?";
        Activity activity = null;

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, activityId);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                activity = new Activity();
                activity.setActivityId(rs.getString("activity_id"));
                activity.setTitle(rs.getString("title"));
                activity.setLocation(rs.getString("location"));
                activity.setStartTime(rs.getTimestamp("start_time"));
                activity.setEndTime(rs.getTimestamp("end_time"));
                activity.setSignUpEndTime(rs.getTimestamp("sign_up_end_time"));
                activity.setMaxParticipants(rs.getInt("max_participants"));
                activity.setCurrentParticipants(rs.getInt("current_participants"));
            }
        } catch (SQLException e) {
            ExceptionUtil.handleSQLException(e);
        }

        return activity;
    }

    // 查询所有活动
    public List<Activity> findAllActivities() {
        String sql = "SELECT * FROM activities";
        List<Activity> activities = new ArrayList<>();

        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                Activity activity = new Activity();
                activity.setActivityId(rs.getString("activity_id"));
                activity.setTitle(rs.getString("title"));
                activity.setLocation(rs.getString("location"));
                activity.setStartTime(rs.getTimestamp("start_time"));
                activity.setEndTime(rs.getTimestamp("end_time"));
                activity.setSignUpEndTime(rs.getTimestamp("sign_up_end_time"));
                activity.setMaxParticipants(rs.getInt("max_participants"));
                activity.setCurrentParticipants(rs.getInt("current_participants"));
                activities.add(activity);
            }
        } catch (SQLException e) {
            ExceptionUtil.handleSQLException(e);
        }

        return activities;
    }

    // 更新活动
    public void updateActivity(Activity activity) {
        String sql = "UPDATE activities SET title = ?, location = ?, start_time = ?, " +
                "end_time = ?, sign_up_end_time = ?, max_participants = ?, " +
                "current_participants = ? WHERE activity_id = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, activity.getTitle());
            pstmt.setString(2, activity.getLocation());
            pstmt.setTimestamp(3, activity.getStartTime());
            pstmt.setTimestamp(4, activity.getEndTime());
            pstmt.setTimestamp(5, activity.getSignUpEndTime());
            pstmt.setInt(6, activity.getMaxParticipants());
            pstmt.setInt(7, activity.getCurrentParticipants());
            pstmt.setString(8, activity.getActivityId());

            pstmt.executeUpdate();
        } catch (SQLException e) {
            ExceptionUtil.handleSQLException(e);
        }
    }
}